//
//  HomeViewController.m
//  OurJourney
//
//  Created by 李沛池 on 2017/3/31.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "HomeViewController.h"
#import "LPCDefine.h"
#import <AVFoundation/AVFoundation.h>
#import "MemoryViewController.h"
#import "CajoleryViewController.h"
#import "LPCSettingManager.h"
#import "LPCInfoManager.h"
#import "LPCAnimationManager.h"
//友盟点击统计
#import <UMMobClick/MobClick.h>
#import "YWGradientView.h"
#import "YWGradientViewAddGradient.h"
#import "YWGradientViewMaskGradient.h"

@interface HomeViewController ()

@end

@implementation HomeViewController
{
    int _currentTextIndex; //当前的文案序号
    int _currentImageIndex; //当前的图片序号
    NSMutableArray *_arrayLoveText; //文案集
    NSMutableArray *_arrayLovePictures; //图片集
    AVAudioPlayer *_audioPlayer; //音乐播放
    LPCSettingManager *_settingManager; //设置监管
    LPCInfoManager *_infoManager; //资源管理
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}


#pragma mark - Init
//初始化数据
- (void)initData
{
    //初始化设置管理
    _settingManager = [LPCSettingManager sharedLPCSettingManager];
    //初始化资源管理
    _infoManager = [LPCInfoManager sharedLPCInfoManager];
    //初始化序号
    _currentTextIndex = 0;
    _currentImageIndex = 0;
    _arrayLoveText = [_infoManager.arrayLoveTexts mutableCopy];
    _arrayLovePictures = [_infoManager.arrayLovePictures mutableCopy];
}



//初始化UI
- (void)initUI
{
    [super initUI];

    //启动音乐
    [self initMusic];
    //启动定时器
    [self initTimer];
}

- (void)initMusic
{
    //后台播放音乐
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    // 1.获取要播放音频文件的URL
    NSURL *fileURL = [[NSBundle mainBundle]URLForResource:@"齐晨-咱们结婚吧" withExtension:@".mp3"];
    // 2.创建 AVAudioPlayer 对象
    _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileURL error:nil];
    // 4.设置循环播放
    _audioPlayer.numberOfLoops = -1;
    // 5.开始播放
//    [_audioPlayer play];
}

- (void)initTimer
{
    //定时更新文案
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeText) userInfo:self repeats:YES];
    //定时更新图片
    [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(changeImageWithAnimation) userInfo:self repeats:YES];
}


#pragma mark - Update
- (void)updateData
{
    //设置文案
    [self changeText];
    //设置图片
    [self changeImage];
    
    NSMutableArray *arrayImages = [@[] mutableCopy];
    for (int i = 0; i < _arrayLovePictures.count; i++) {
        NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"LovePictures.bundle/%@", _arrayLovePictures[i]]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
            imagePath = [PATH_LovePictures stringByAppendingPathComponent:_arrayLovePictures[i]];
        }
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [arrayImages addObject:image];
    }
//    [YWGradientView gradientViewWithFrame:(CGRect){CGPointZero,CGSizeMake(_loveImageView.frame.size.width, _loveImageView.frame.size.height)} WithImages:arrayImages joinInSuperView:_loveImageView];
}

//更新文案
- (void)changeText
{
    if (_settingManager.isRandomText) { //如果随机走随机
            int currentRandomNum = arc4random()%_arrayLoveText.count;
            _loveLabel.text = _arrayLoveText[currentRandomNum];
            //随机选择后移除 避免随机重复率过高
            [_arrayLoveText removeObjectAtIndex:currentRandomNum];
        //如果数据全部展示一次 重置数据
        if (_arrayLoveText.count == 0) {
            _arrayLoveText = nil;
            _arrayLoveText = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LovePlist" ofType:@"plist"]];
        }
    }else { //顺序
        _loveLabel.text = _arrayLoveText[_currentTextIndex++];
        if (_currentTextIndex >= _arrayLoveText.count) {
            _currentTextIndex = 0;
        }
    }
}

//定时更新图片带动画效果
- (void)changeImageWithAnimation
{
    [LPCAnimationManager randomChangeImageAnimation:_loveImageView];
    
    [self changeImage];
}

//更新图片
- (void)changeImage
{
    NSString *currentImageName = @"";
    if (_settingManager.isRandomImage) {
        int currentRandomNum = arc4random()%_arrayLovePictures.count;
        currentImageName = _arrayLovePictures[currentRandomNum];
        //随机选择后移除 避免随机重复率过高
        [_arrayLovePictures removeObjectAtIndex:currentRandomNum];
        //如果数据全部展示一次 重置数据
        if (_arrayLovePictures.count == 0) {
            _arrayLovePictures = [_infoManager.arrayLovePictures mutableCopy];
        }
    }else {
        currentImageName = _arrayLovePictures[_currentImageIndex++];
        if (_currentImageIndex >= _arrayLovePictures.count) {
            _currentImageIndex = 0;
        }
    }
    NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"LovePictures.bundle/%@", currentImageName]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
        imagePath = [PATH_LovePictures stringByAppendingPathComponent:currentImageName];
    }
    _loveImageView.image = [UIImage imageWithContentsOfFile:imagePath];
}


#pragma mark - Action
//点点滴滴
- (IBAction)showPictureList:(UIButton *)sender
{
    [MobClick event:@"LovePictures"];
    MemoryViewController *memoryVC = XIBFromStoryBoard(@"MemoryViewController");
    memoryVC.arrayData = _arrayLovePictures;
    [self presentViewController:memoryVC animated:YES completion:^{
        
    }];
}

//甜言蜜语
- (IBAction)showTextList:(UIButton *)sender
{
    [MobClick event:@"LoveTexts"];
    CajoleryViewController *cajoleryVC = [[CajoleryViewController alloc] init];
    cajoleryVC.arrayData = _arrayLoveText;
    [self presentViewController:cajoleryVC animated:YES completion:^{
        
    }];
}

//播放/暂停音乐
- (IBAction)playMusic:(UIButton *)sender
{
    if (_audioPlayer.playing) {
        [MobClick event:@"pauseMusic"];
        [_audioPlayer pause];
    }else {
        [MobClick event:@"playMusic"];
        [_audioPlayer play];
    }
}










@end
